/**
 *Project: jh-framework
 *File: cn.xkshow.framework.spider.baseIface.HtmlCode.java 
 *Author: admin@xkshow.cn with the copyrite
 ****************************************************************
 *   VERSION       DATE            BY       CHANGE/COMMENT
 * --------------------------------------------------------------
 *    1.0        2013-4-19        xkshow.cn         create
 ****************************************************************
 *note:
 ***************************************************************/ 
package cn.xkshow.commons.spider.baseIface;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlCode {
	private String url;
	private String charcode;
	
	public HtmlCode(String url, String code) {
		this.url = url;
		this.charcode = code;
	}

	public String getHtmlStr(){
		try {
			URL url = new URL(this.getUrl());
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.connect();
			InputStream is = conn.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, this.getCharcode()));
			br.mark(100);
			String line = "";
			StringBuffer resultBuffer = new StringBuffer();
			while ((line = br.readLine()) != null) {
				resultBuffer.append(line.trim());
			}
			br.close();
			is.close();
			conn.disconnect();
			return resultBuffer.toString().toLowerCase();
		} catch (Exception e) {
		}
		return null;
	}
	
	/**获取正则匹配的字符串*/
	public String getPattern(String regx ,String html) {
		StringBuffer regxHtml = new StringBuffer();
		Pattern p = Pattern.compile(regx.toLowerCase());
		Matcher m = p.matcher(html);
		while (m.find()) {
			regxHtml.append(m.group());
		}
		return regxHtml.toString();
	}
	/**获取列表*/
	public List getPatterArrayList(String regx,String html) {
		List list = new ArrayList();
		Pattern p = Pattern.compile(regx.toLowerCase());
		Matcher m = p.matcher(html);
		while (m.find()) {
			list.add(m.group());
		}
		return list;
	}
	
	public String[] getArrayList (String regex,String html) {
		String[] list = html.split(regex);
		return list;
		
	}

	/**
	 * getter - setter
	 */
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getCharcode() {
		return charcode;
	}

	public void setCharcode(String charcode) {
		this.charcode = charcode;
	}
	
}
